package com.shenluyao.contrller;


import com.shenluyao.common.HxConstants;
import com.shenluyao.common.Md5Utils;
import com.shenluyao.common.ResultDTO;
import com.shenluyao.model.FpsPerUserArg;
import com.shenluyao.model.SystUserInfo;
import com.shenluyao.service.FpsPerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author Allen
 * @Description: 用户
 * @date 2018/11/15 16:10
 */

@RestController
@RequestMapping(value = "/lyen/httpServerfps")
public class FpsPerUserController {

    @Autowired
    private FpsPerUserService fpsPerUserService;

    /**
     * 远程新增用户
     * @param baseEntity
     * @return
     */
    @RequestMapping(value = "/remotecreatefps.xhdx", method= RequestMethod.GET)
    @ResponseBody
    public ResultDTO<?> create(FpsPerUserArg baseEntity) {
        ResultDTO<FpsPerUserArg> resultDTO = new ResultDTO<FpsPerUserArg>();
        if(baseEntity==null){
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_PARAMETER_NULL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_PARAMETER_NULL);
            return resultDTO;
        }
        String id = baseEntity.getId();
        String loginId = baseEntity.getLoginId();
        if (null == id || "".equals(id)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("id不能为空，必须输入！");
            return resultDTO;
        }
        if (null == loginId || "".equals(loginId)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("loginId不能为空，必须输入！");
            return resultDTO;
        }
        //检查密码
        if (checkPwd(baseEntity, resultDTO)) {
            return resultDTO;
        }
        try{
            //检查id
            FpsPerUserArg pdtCate2 = new FpsPerUserArg();
            pdtCate2.setIsRemoved("0");
            pdtCate2.setId(baseEntity.getId());
            List<FpsPerUserArg> pmId = fpsPerUserService.findFpsPerUser(pdtCate2);
            if(null != pmId && pmId.size()>0){
                resultDTO.setReturnCode(HxConstants.RETURN_CODE_DATA_EXIST);
                resultDTO.setReturnMsg("该用户的id为"+id+"已经被注册，请重新输入！");
                return resultDTO;
            }

            //检查loginId
            FpsPerUserArg pdtCate = new FpsPerUserArg();
            pdtCate.setIsRemoved("0");
            pdtCate.setLoginId(baseEntity.getLoginId());
            List<FpsPerUserArg> pmLoginId = fpsPerUserService.findFpsPerUser(pdtCate);
            if(null != pmLoginId && pmLoginId.size()>0){
                resultDTO.setReturnCode(HxConstants.RETURN_CODE_DATA_EXIST);
                resultDTO.setReturnMsg("该用户的账号LoginId为"+loginId+"已经被注册，请重新输入！");
                return resultDTO;
            }

            baseEntity.setIsRemoved("0");
            baseEntity.setCreateDate(new Date());
            baseEntity.setIsActivate("0");
            baseEntity.setIsStop("0");
            //创建用户
            baseEntity.setOrganizationId(baseEntity.getServiceUnit());
            fpsPerUserService.insert(baseEntity);
            SystUserInfo sy = new SystUserInfo();
            sy.setCreator(baseEntity.getId());
            sy.setServiceUnit("-1".equalsIgnoreCase(baseEntity.getServiceUnit())?"":baseEntity.getServiceUnit());
            sy.setId(baseEntity.getId());
            sy.setIntegralValue(new BigDecimal(0));
            sy.setIntegralValueAll(new BigDecimal(0));
            fpsPerUserService.createSystUserInfoById(sy);
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SUCCESS);
            resultDTO.setReturnMsg("用户新增成功");
        }catch(Exception e){
            e.printStackTrace();
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SYSTEM_FAIL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_SYSTEM_FAIL);
            return resultDTO;
        }
        return resultDTO;
    }

    private boolean checkPwd(FpsPerUserArg baseEntity, ResultDTO<FpsPerUserArg> resultDTO) {
        if (null != baseEntity.getUserPassword() && !"".equals(baseEntity.getUserPassword())) {
            String pwd = Md5Utils.MD5Encode(baseEntity.getUserPassword(), "utf8");
            baseEntity.setUserPassword(pwd);
        } else {
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_PWD_NULL);
            resultDTO.setReturnMsg("账号的密码不能为空！");
            return true;
        }
        return false;
    }

    /**
     * 远程修改用户
     * @param baseEntity
     * @return
     */
    @RequestMapping(value = "/remoteupdatefps.xhdx", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<?> update(FpsPerUserArg baseEntity) {
        ResultDTO<FpsPerUserArg> resultDTO = new ResultDTO<>();
        if (baseEntity == null) {
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_PARAMETER_NULL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_PARAMETER_NULL);
            return resultDTO;
        }
        String id = baseEntity.getId();
        String loginId = baseEntity.getLoginId();
        if (null == id || "".equals(id)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("id不能为空，必须输入！");
            return resultDTO;
        }
        if (null == loginId || "".equals(loginId)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("loginId不能为空，必须输入！");
            return resultDTO;
        }
        //检查密码
        if (checkPwd(baseEntity, resultDTO)) {
            return resultDTO;
        }
        try {
            //检查id
            FpsPerUserArg pdtCate2 = new FpsPerUserArg();
            pdtCate2.setIsRemoved("0");
            pdtCate2.setId(baseEntity.getId());
            List<FpsPerUserArg> pmId = fpsPerUserService.findFpsPerUser(pdtCate2);
            if (null == pmId || pmId.size() == 0) {
                resultDTO.setReturnCode(HxConstants.RETURN_CODE_DATA_NULL);
                resultDTO.setReturnMsg("id为"+id+"用户不存在，请查证！");
                return resultDTO;
            }
            //检查loginId
            FpsPerUserArg pdtCate = new FpsPerUserArg();
            pdtCate.setIsRemoved("0");
            pdtCate.setLoginId(baseEntity.getLoginId());
            List<FpsPerUserArg> pmLoginId = fpsPerUserService.findFpsPerUser(pdtCate);
            if (null != pmLoginId && pmLoginId.size() > 0) {
                String checkId = pmLoginId.get(0).getId();
                if (!id.equals(checkId)){
                    resultDTO.setReturnCode(HxConstants.RETURN_CODE_DATA_EXIST);
                    resultDTO.setReturnMsg("用户的LoginId为"+loginId+"不能重复，请重新输入！");
                    return resultDTO;
                }
            }
            baseEntity.setIsRemoved("0");
            //修改用户
            baseEntity.setOrganizationId(baseEntity.getServiceUnit());
            fpsPerUserService.updateByPrimaryKeySelective(baseEntity);
            SystUserInfo sy = new SystUserInfo();
            sy.setCreator(baseEntity.getId());
            sy.setServiceUnit("-1".equalsIgnoreCase(baseEntity.getServiceUnit()) ? "" : baseEntity.getServiceUnit());
            sy.setId(baseEntity.getId());
            fpsPerUserService.updateSystUserInfoById(sy);
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SUCCESS);
            resultDTO.setReturnMsg("用户修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SYSTEM_FAIL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_SYSTEM_FAIL);
            return resultDTO;
        }
        return resultDTO;
    }


    /**
     * 查询用户数据接口
     *
     * @param baseEntity 入参参数
     * @return
     */
    @RequestMapping(value = "/remoteFindFpsPage.xhdx", method = RequestMethod.GET)
    @ResponseBody
    public List<FpsPerUserArg> findFpsPage(FpsPerUserArg baseEntity) {
        List<FpsPerUserArg> fpsPerUserList = fpsPerUserService.findFpsPerUser(baseEntity);
        return fpsPerUserList;
    }

    /**
     * 删除用户数据接口
     * http:// 112.126.66.169:8083/lyen/httpServerfps/remotedeletefpsbynames.xhdx;
     * 参数：String（用户ID）
     * Type:  get
     * 格式：” admin,00868708,student1,00857376……”
     * @param
     * @return
     */
    @RequestMapping(value = "/remotedeletefpsbynames.xhdx", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<?> deletefpsbynames(String names) {
        ResultDTO<FpsPerUserArg> resultDTO = new ResultDTO<>();
        if(null == names || "".equals(names)) {
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SYSTEM_FAIL);
            resultDTO.setReturnMsg("参数names不能为空！");
            return resultDTO;
        }
        if(!names.contains(",")){
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SYSTEM_FAIL);
            resultDTO.setReturnMsg("参数格式错误，格式为“names='admin,00868708,student1,00857376……'”！");
            return resultDTO;
        }
        String[] namesArray = names.split(",");
        if(namesArray.length%2 != 0) {
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SYSTEM_FAIL);
            resultDTO.setReturnMsg("参数格式错误，格式为“names='admin,00868708,student1,00857376……'”！");
            return resultDTO;
        }
        FpsPerUserArg fpsPerUserArg = new FpsPerUserArg();
        List<FpsPerUserArg> fpsPerUserArgList = null;
        for (int i = 0; i < namesArray.length; i = i+2) {
            // 每两个是一条数据
            if(null != namesArray[i] && !"".equals(namesArray[i])){
                fpsPerUserArg.setUserName(namesArray[i]);
            }
            if(null != namesArray[i+1] && !"".equals(namesArray[i+1])){
                fpsPerUserArg.setLoginId(namesArray[i+1]);
            }
            if(null != fpsPerUserArg){
                fpsPerUserArgList = fpsPerUserService.findFpsPerUser(fpsPerUserArg);
                // 数据存在则删除
                if(fpsPerUserArgList.size() > 0){
                    fpsPerUserService.deleteByNames(fpsPerUserArg);
                }
            }
        }
        resultDTO.setReturnCode(HxConstants.RETURN_CODE_SUCCESS);
        resultDTO.setReturnMsg("用户删除成功");
        return resultDTO;
    }
}
